# Maintainer: KokaKiwi <kokakiwi+aur@kokakiwi.net>
# Contributor: George Rawlinson <george@rawlinson.net.nz>

# Config
((DISABLE_DOTNET)) || optdepends+=('dotnet-runtime: support for .NET scripts') makedepends+=('dotnet-runtime')

_pkgname=ImHex
pkgname=${_pkgname,,}
pkgver=1.32.2
pkgrel=1
pkgdesc='A Hex Editor for Reverse Engineers, Programmers and people that value their eye sight when working at 3 AM'
url='https://imhex.werwolv.net'
license=('GPL2')
arch=('x86_64')
depends=('glfw' 'mbedtls' 'curl' 'dbus'
         'freetype2' 'file' 'hicolor-icon-theme' 'xdg-desktop-portal'
         'fmt' 'yara' 'capstone')
makedepends+=('git' 'cmake'
             'llvm' 'librsvg' 'nlohmann-json'
             'python' 'cli11')
provides=('imhex-patterns')
conflicts=('imhex-patterns-git')
source=("imhex-${pkgver}.tar.gz::https://github.com/WerWolv/ImHex/releases/download/v${pkgver}/Full.Sources.tar.gz"
        "imhex-patterns.tar.gz::https://github.com/WerWolv/ImHex-Patterns/archive/refs/tags/ImHex-v${pkgver}.tar.gz"
        0001-fix-cmake-Fix-when-multiple-.NET-packages-are-instal.patch
        0002-fix-main-Handle-different-LLVM-version.patch)
sha512sums=('72e3fda050220fa9098f4ad9ed1777db565c81549daf09a5d113e35036d26780ab50c5a45ddf97ec6daa22e832a0d0b916097082706bd7314d56c2f97abcbb11'
            '3f6d1c874e1d6bc4e2e035361c5c7c1da4374cfcaeb6123f187302cacf91ce781db3f4fbe74e259e1bdb18418987fe48067365dcc3324190fbadcaf0b5ab9333'
            'af7a0b341a3ef5cb99b3a7d4f522610fa32d7af7034334ef6b88601d6d69c4749d71094ed6ddd155428feade244ded369b5f24f9ab0eec4ce1a0887eff6310ee'
            '702abff3858d77c617569c428a87521208901cf743b52f537b904ea484d58b6272558d6eb922a178b8e5e7f325271851d2fe08bfc8870d6c876def0c0ad79206')
options=(!lto !strip)

prepare() {
  cd "${_pkgname}"

  if ((!DISABLE_DOTNET)); then
    patch -p1 < "$srcdir/0001-fix-cmake-Fix-when-multiple-.NET-packages-are-instal.patch"
  fi

  patch -p1 < "$srcdir/0002-fix-main-Handle-different-LLVM-version.patch"
}

build() {
  export CXXFLAGS="$CXXFLAGS -Wno-inconsistent-missing-override"

  cmake -B build -S "${_pkgname}" \
    -Wno-dev \
    -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_INSTALL_PREFIX=/usr \
    -D CMAKE_SKIP_RPATH=ON \
    -D IMHEX_OFFLINE_BUILD=ON \
    -D IMHEX_IGNORE_BAD_CLONE=ON \
    -D IMHEX_STRIP_RELEASE=OFF \
    -D IMHEX_STRICT_WARNINGS=OFF \
    -D IMHEX_BUNDLE_DOTNET=OFF \
    -D USE_SYSTEM_LLVM=ON \
    -D USE_SYSTEM_YARA=ON \
    -D USE_SYSTEM_FMT=ON \
    -D USE_SYSTEM_NLOHMANN_JSON=ON \
    -D USE_SYSTEM_CAPSTONE=ON \
    -D USE_SYSTEM_CLI11=ON \
    -D IMHEX_VERSION="$pkgver"

  cmake --build build
}

package() {
  DESTDIR="$pkgdir" cmake --install build

  # Patterns
  install -dm0755 "$pkgdir/usr/share/imhex"
  cp -r -t "$pkgdir/usr/share/imhex" \
    "$srcdir/ImHex-Patterns-ImHex-v${pkgver}"/{constants,encodings,includes,magic,nodes,patterns,plugins,scripts,tests,themes,tips,yara}

  # Desktop file(s)
  install -Dm0644 "${_pkgname}/resources/icon.svg" "$pkgdir/usr/share/icons/hicolor/scalable/apps/imhex.svg"
  for size in 32 48 64 128 256; do
    install -dm0755 "$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps"
    rsvg-convert -a -f png -w $size -o "$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps/imhex.png" \
      "${_pkgname}/resources/icon.svg"
  done

  # Documentation
  install -Dm0644 -t "$pkgdir/usr/share/doc/$pkgname" \
    "${_pkgname}/README.md"
}
